// 版权归陈超所有


#include "Characters/CC_HeroCharacter.h"

// #include "AbilitySystemComponent.h"
#include "AbilitySystem/CC_AbilitySystemComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Player/CC_PlayerController.h"
#include "Player/CC_PlayerState.h"
#include "UI/HUD/CC_HUD.h"

ACC_HeroCharacter::ACC_HeroCharacter()
{
	//角色本身的设置（俯视角游戏）
	GetCharacterMovement()->bOrientRotationToMovement = true; //设置为true，角色将朝移动的方向旋转
	GetCharacterMovement()->RotationRate = FRotator(0.f, 400.f, 0.f); //旋转速率
	GetCharacterMovement()->bConstrainToPlane = true; //约束到平面
	GetCharacterMovement()->bSnapToPlaneAtStart = true; //设置了上面一项为true，且此项设置为true，则在开始时与地面对齐

	bUseControllerRotationPitch = false;	//同时需要确保角色本身不跟随控制器旋转：
	bUseControllerRotationYaw = false;
	bUseControllerRotationRoll = false;
	
	//弹簧臂组件
	SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
	SpringArm->SetupAttachment(RootComponent);	//设置附着点（跟组件）
	SpringArm->TargetArmLength = 900;
	SpringArm->SetRelativeRotation(FRotator(-45.f, 0, 0));	//设置相对旋转 设置俯视角度（Pitch负值）
	SpringArm->bUsePawnControlRotation = false;	//确保不随角色转向而转动
	SpringArm->bInheritPitch = false;	//取消继承俯仰
	SpringArm->bInheritRoll = false;	//取消继承翻滚
	SpringArm->bInheritYaw = false;		//取消继承摇摆
	SpringArm->bEnableCameraLag = true;	//相机延迟效果
		
	//跟随摄像机组件
	FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
	FollowCamera->SetupAttachment(SpringArm, USpringArmComponent::SocketName); //设置附着点（弹簧臂组件接口名称）
	FollowCamera->bUsePawnControlRotation = false;
	

	
}

void ACC_HeroCharacter::PossessedBy(AController* NewController)
{
	Super::PossessedBy(NewController);
	//服务器端初始化ASC
	InitCharacterASC();

	AddCharacterAbilities();		//添加能力
}

void ACC_HeroCharacter::OnRep_PlayerState()
{
	Super::OnRep_PlayerState();
	//客户端初始化ASC
	InitCharacterASC();
}

void ACC_HeroCharacter::InitCharacterASC()
{
	if (CCPlayerState == nullptr)
	{
		CCPlayerState = GetPlayerState<ACC_PlayerState>();	//获取玩家状态
	}
	// check(CCPlayerState);
	if (CCPlayerState == nullptr)
	{
		UE_LOG(LogTemp, Warning, TEXT("CCPlayerState: %s - CCPlayerState is nullptl"), *GetName());
		return;
	}
	//获取玩家角色的ASC、AS
	if (AbilitySystemComponent == nullptr || AttributeSet == nullptr)
	{
		AbilitySystemComponent = CCPlayerState->GetAbilitySystemComponent();
		AttributeSet = CCPlayerState->GetAttributeSet();
	}
	//初始化ASC
	AbilitySystemComponent->InitAbilityActorInfo(CCPlayerState, this);
	Cast<UCC_AbilitySystemComponent>(AbilitySystemComponent)->AbilityActorInfoSet();
	//获取玩家控制器
	ACC_PlayerController* CCPlayerController = Cast<ACC_PlayerController>(GetController());
	/*服务器中，拥有所有玩家控制器
	 * 客户端只拥有自己的玩家控制器,当控制器为空是，游戏不能崩溃
	 */
	if (CCPlayerController)
	{
		//获取HUD
		ACC_HUD* CCHUD = Cast<ACC_HUD>(CCPlayerController->GetHUD());
		if (CCHUD) //仅对本地控制的玩家有效
		{
			//初始化HUD
			CCHUD ->InitOverlay(CCPlayerController, CCPlayerState, AbilitySystemComponent, AttributeSet);
		}
	}

	InitCharacterDefaultAttribute();		//初始化角色属性
}

int32 ACC_HeroCharacter::GetCharacterLevel()
{
	if (CCPlayerState == nullptr)
	{
		CCPlayerState = GetPlayerState<ACC_PlayerState>();	//获取玩家状态
	}
	return CCPlayerState->GetCharacterLevel();
}
